<div class="page-body">
    <div class="row">
        <div class="widget">
            <div class="widget-header
            <switch name="T_type">
            <case value="1">bg-magenta</case>
            <case value="2">bg-palegreen</case>
            <case value="3">bg-lightred</case>
            <case value="4">bg-blue</case>
            <case value="5">bg-blueberry</case>
            <case value="6">bg-gold</case>
            <default />bg-magenta
            </switch>
                                ">
            <span class="widget-caption">{$T_title|default=CONTROLLER_NAME}</span>
                                <!--Widget Buttons-->
        </div>
            <div class="widget-body">
                <div id="horizontal-form">
                    <form class="form-horizontal form_buy" role="form"
                          action="{:U('doApply')}"
                          method="post" enctype="multipart/form-data" onSubmit="return false;">
                        <div class="form-group">
                            <label for="card_id" class="col-sm-3 control-label no-padding-right">会员卡：</label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control" id="card_id" name="card_id" value="" onkeydown="getCard()" >

                                <div id="card_info" style="display:none;">
                                    <span class="info"></span><br>
                                    购书折扣：<span class="discount_buy"></span><br>
                                    活动报名费折扣：<span class="discount_apply"></span><br>
                                    借阅可免运费次数：<span class="num_delivery"></span><br>
                                    活动报名费可免次数：<span class="num_activity"></span><br>
                                </div>
                            </div>
                        </div>
                        <hr>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">
                                报名类型：</label>
                            <div class="col-sm-3">
                                <select id="style" name="style" >
                                    <option value="0">会员</option>
                                    <option value="1">非会员</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">
                                活动：</label>
                            <div class="col-sm-3">
                                <select id="activity_id" name="activity_id" onchange="selAct()">
                                    <option value="0">—— 请选择 ——</option>
                                    <volist name="act" id="v">
                                        <option value="{$v.activity_id}">{$v.title}</option>
                                    </volist>
                                </select>
                            </div>
                        </div>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right"></label>
                            <div class="col-sm-6">
                                <table class="table table-hover" aria-describedby="simpledatatable_info">
                                    <thead class="bordered-darkorange">
                                    <tr>
                                        <th>活动名</th>
                                        <th>总人数</th>
                                        <th>剩余报名</th>
                                        <th>活动费/折扣价</th>
                                        <th>材料费</th>
                                        <th>{:C('act_freenum_name')}</th>
                                    </tr>
                                    </thead>
                                    <tbody class="activity_info">

                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label no-padding-right">联系人：</label>
                            <div class="col-sm-3">
                                <input type="text" id="contactor_name" name="contactor_name" class="form-control" value="" >
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label no-padding-right">联系电话：</label>
                            <div class="col-sm-3">
                                <input type="text" id="contactor_phone" name="contactor_phone" class="form-control" value="" onkeyup="formatphone($(this))" >
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label no-padding-right">总金额：</label>
                            <div class="col-sm-3">
                                <input type="number" id="money" name="money" class="form-control" value="" readonly="true" >
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label no-padding-right">实收：</label>
                            <div class="col-sm-3">
                                <input type="number" id="money_real" name="money_real" class="form-control" value="" onkeyup="cal_zero()" onclick="cal_zero()">
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label no-padding-right">找零：</label>
                            <div class="col-sm-3">
                                <input type="number" id="money_zero" name="money_zero" class="form-control" value="" >
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">支付方式：</label>
                            <div class="col-sm-3">
                                <select name="pay_way" id="pay_way">
                                </select>
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>
                        <div class="form-group">

                            <div class="col-sm-offset-3 col-sm-10">
                                <p class="label label-darkpink graded error_info"></p><br>
                                <input type="button" class="btn btn-primary" value="提交" onkeypress="FSubmit()" onclick="FSubmit()">
                                <input type="hidden" id="user_id" name="user_id" value="" >
                                <!--报名费-->
                                <input type="hidden" id="price_apply" name="price_apply" value="" >
                                <!--折后报名费-->
                                <input type="hidden" id="price_apply_d" name="price_apply_d" value="" >
                                <!--材料费-->
                                <input type="hidden" id="price_material" name="price_material" value="" >
                                <!--是否免报名费-->
                                <input type="hidden" id="is_applyfree" name="is_applyfree" value="0" >
                                <!--报名费折扣-->
                                <input type="hidden" id="discount_apply" name="discount_apply" value="1" >
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    //获取会员卡成功后回调
    function getsuccess(data){
        $("#user_id").val(data.data.user_id);
        $("#discount_buy").val(data.data.discount_buy);
        $("#contactor_name").val(data.data.child_name);
        $("#contactor_phone").val(data.data.father_phone);
        if(data.data.father_phone==''){
            $("#contactor_phone").val(data.data.mother_phone);
        }
        formatphone($("#contactor_phone"));

        $("#card_info").show();
        $("#activity_id").focus();
    }
    //添加活动信息
    function selAct() {
        var $activity_id=$("#activity_id").val();
        if($activity_id==0){
            $(".activity_info").html("");
            return;
        }
            $.post("{:U('Apply/act_search')}",{"activity_id":$activity_id},function(data){
                if(data.status=="error"){
                    alert(data.data);
                }else{
                        $str = '<tr class="active">'+
                                '<td title="'+data.data.title+'">'+data.data.title+'</td>'+
                                '<td>'+data.data.apply_total+'</td>' +
                                '<td>'+data.data.apply_num+'</td>'+
                                '<td>￥<s class="price_apply">'+data.data.price_apply+'</s>元&nbsp;&nbsp;/￥<span class="price_apply_d"></span>元</td>'+
                                '<td>￥<span class="price_material">'+data.data.price_material+'</span>元</td>' +
                                '<td><span class="num_deduct">'+data.data.num_deduct+'</span>次</td>' +
                                '</tr>';
                    $(".activity_info").html($str);
                    cal_all();
                }
            },"json");
    }
    //计算总金额及活动折扣价等
    function cal_all(){
        var $num_activity=parseInt($(".num_activity").html());  //免活动费次数
        var $num_deduct=parseInt($(".num_deduct").html());  //免活动费次数
        var $discount_apply=$(".discount_apply").html(); //活动费折扣

        if($num_activity=="" ){
            $num_activity=0;
        }
        if($discount_apply==""){
            $discount_apply=1;
        }

        var $item=$(".activity_info tr");
        var $m_apply=$item.find(".price_apply").html();//原活动费金额
        var $m_apply_d=0;//折扣活动费
        var $m_material=$item.find(".price_material").html(); //材料费
        var $m_total=0; //支付总金额
        if($m_apply=="" || isNaN($m_apply)){
            $m_apply=0;
        }else{
            $m_apply=parseFloat($m_apply).toFixed(2);
        }
        if($m_material=="" || isNaN($m_material)){
            $m_material=0;
        }else{
            $m_material=parseFloat($m_material).toFixed(2);
        }
        if($num_activity>=$num_deduct){
            log('去掉');
            $m_apply_d=0;
            $("#is_applyfree").val(1);
        }else{
            log('没');
            $m_apply_d=parseFloat($m_apply*$discount_apply).toFixed(2);
            $("#is_applyfree").val(0);
            $("#discount_apply").val($discount_apply);
        }
        log('星星章:'+$num_activity);
        log('所需星星章:'+$num_deduct);
        $m_total=parseFloat(parseFloat($m_apply_d)+parseFloat($m_material)).toFixed(2);

        $item.find(".price_apply_d").html($m_apply_d);
        $("#price_apply").val($m_apply);
        $("#price_apply_d").val($m_apply_d);
        $("#price_material").val($m_material);
        $("#money").val($m_total);
        cal_zero();
    }
    //计算找零
    function cal_zero(){
        var $m_total=$("#money").val();//实收
        if($m_total==""){
            $m_total=0;
        }
        var $m_real=$("#money_real").val();//实收
        if($m_real==""){
            $m_real=0;
        }
        var $m_zero=$m_real-$m_total;//找零
        $("#money_zero").val($m_zero.toFixed(2));
    }

    function formatphone($this){
        $this.val(checkphone($this.val()));
    }

    //提交
    function FSubmit(){
//        getCard_post();
//        cal_all();
        if(!confirm('确认操作?')){
            return;
        }
        if($("#user_id").val()=="" && $('#style').val()<1){
            $(".error_info").html("请输入会员卡");
            return;
        }
        if($("#activity_id").val()==0){
            $(".error_info").html("请选择活动");
            return;
        }
        if($("#contactor_name").val()=="" ||  $("#contactor_phone").val()==""){
            $(".error_info").html("联系人和电话必填");
            return;
        }
        if($("#money_real").val()=="" || parseFloat($("#money_zero").val())<0){
            $(".error_info").html("请输入实收金额或找零错误");
            return;
        }
        var $activity_id=$("#activity_id").val();
        var $user_id=$("#user_id").val();
        var $style=$('#style').val();

        $.post("{:U('applyCheck')}",{"activity_id":$activity_id,"user_id":$user_id,'style':$style},function(data){
            if(data.status=="success"){
                $(".form_buy").attr("onSubmit","");
                $(".form_buy").submit();
                return true;
            }else{
                alert(data.data);
            }
        },"json");
    }
</script>